package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.selfdriving.R;
import com.selfdriving.bean.User;
import com.selfdriving.dialog.MessageDialog;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.SystemSettings;

public class RegisterThreeActivity extends BaseActivity implements
		OnClickListener
{
	@ViewInject(id = R.id.top_text_name)
	TextView top_text_name;

	@ViewInject(id = R.id.top_text_step)
	TextView top_text_step;

	@ViewInject(id = R.id.btn_back, click = "btn_back")
	Button btn_back;

	@ViewInject(id = R.id.btn_ok, click = "btn_ok")
	Button btn_ok;

	private MessageDialog dialog;
	private EditText user;
	private EditText password1;
	private EditText password2;
	String phone;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_three);
		Intent intent = super.getIntent();
		dialog = new MessageDialog(this, "提示", "选择性别后可以选择完成注册或者进入完善资料", "完成注册",
				"完善资料", true, true, this);
		phone = intent.getStringExtra("phone");
		user = (EditText) findViewById(R.id.reg_baseinfo_et_name);
		password1 = (EditText) findViewById(R.id.reg_baseinfo_et_pass);
		password2 = (EditText) findViewById(R.id.reg_baseinfo_et_pass1);
		top_text_name.setText("填写注册信息");
		top_text_step.setText("2/2");
	}

	public void btn_back(View v)
	{
		this.finish();
	}

	public void btn_ok(View v)
	{
		String userName = user.getText().toString();
		String pass1 = password1.getText().toString();
		String pass2 = password2.getText().toString();
		if (userName.length() < 1)
		{
			showToast("请输入您的用户名/昵称");
			return;
		}
		if (pass1.length() < 1)
		{
			showToast("请输入您的密码");
			return;
		}
		if (pass2.length() < 1)
		{
			showToast("请确认您的密码");
			return;
		}
		if (!pass1.equals(pass2))
		{
			showToast("两次输入的密码不一致");
			return;
		}
		register(userName, pass1);
	}

	@Override
	public void onClick(View v)
	{
		Intent intent = new Intent();
		switch (v.getId())
		{
		case R.id.dialog_generic_btn_button1:
			dialog.dismiss();
			intent.setClass(this, HomePageActivity.class);
			startActivity(intent);
			break;
		case R.id.dialog_generic_btn_button2:
			intent.setClass(this, VerifyActivity.class);
			intent.putExtra("type", 1);
			startActivity(intent);
			dialog.dismiss();
			break;

		default:
			break;
		}

	}

	public void register(final String name, final String pwd)
	{
		showLoadingDialog("注册中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "12");
		params.put("phone", phone);
		params.put("name", name);
		params.put("pwd", pwd);
		params.put("lng", "" + mApplication.mLongitude);
		params.put("lat", "" + mApplication.mLatitude);
		params.put("city", mApplication.city);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(100000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				showToast("注册失败");
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString();
				if (json.contains("111111"))
				{
					showToast("注册失败，改手机已被注册");
					dismissLoadingDialog();
				} else
				{
					showToast("注册成功");
					mApplication.user = new User(
							"http://117.21.209.117:9000/driving/files/user.jpg",
							phone, name, pwd, mApplication.mLatitude,
							mApplication.mLongitude);
					SystemSettings.putString(RegisterThreeActivity.this,
							"phone", phone);
					SystemSettings.putString(RegisterThreeActivity.this, "pwd",
							pwd);
					dismissLoadingDialog();
					dialog.show();
				}
			}
		});
	}
}
